{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "437dd26e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-03T04:02:21.501453800Z",
     "start_time": "2024-07-03T04:02:16.557383700Z"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "import requests\n",
    "import time\n",
    "\n",
    "#创建浏览器操作对象\n",
    "driver = webdriver.Edge()\n",
    "url = \"https://www.zcool.com.cn/tag/ZNTI3NjAyOA==.html\"\n",
    "#访问网站\n",
    "driver.get(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "# 滚动页面\n",
    "driver.execute_script(\"window.scrollBy(0, 5000);\")\n",
    "# time.sleep(5)\n",
    "# \n",
    "# driver.execute_script(\"window.scrollBy(0, 5000);\")\n",
    "# time.sleep(5)\n",
    "# \n",
    "# driver.execute_script(\"window.scrollBy(0, 5000);\")\n",
    "# time.sleep(5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-03T04:02:21.532314200Z",
     "start_time": "2024-07-03T04:02:21.502453300Z"
    }
   },
   "id": "ed88b5d4462a5196"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1保存成功\n",
      "2保存成功\n",
      "3保存成功\n",
      "4保存成功\n",
      "5保存成功\n",
      "6保存成功\n",
      "7保存成功\n",
      "8保存成功\n",
      "9保存成功\n",
      "10保存成功\n",
      "11保存成功\n",
      "12保存成功\n",
      "13保存成功\n",
      "14保存成功\n",
      "15保存成功\n",
      "16保存成功\n",
      "17保存成功\n",
      "18保存成功\n"
     ]
    }
   ],
   "source": [
    "#获取当前页面图片src\n",
    "button = driver.find_elements(By.XPATH,\"//div[@class='sc-cc6obj-0 boekug content-box_card_item']/div/a/img\")  \n",
    "k=0\n",
    "for i in button:\n",
    "    src = i.get_attribute('src')\n",
    "    url = src\n",
    "\n",
    "   # UA伪装\n",
    "    headers = {\n",
    "    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'\n",
    "    }\n",
    "    resp = requests.get(url=url,headers=headers).content#图片内容\n",
    "    time.sleep(2)\n",
    "    k= k+1\n",
    "    with open(str(k) +'.jpg','wb+') as f:#保存图片    \n",
    "        f.write(resp)#写入图片\n",
    "        print(str(k)+\"保存成功\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-03T04:03:01.829235600Z",
     "start_time": "2024-07-03T04:02:21.534813200Z"
    }
   },
   "id": "60d49e014f28396d"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "# 关闭驱动，释放资源\n",
    "driver.close()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-03T04:03:01.998720700Z",
     "start_time": "2024-07-03T04:03:01.830235900Z"
    }
   },
   "id": "cf91ea033ca60745"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
